Ext.define('AmpleHarvest.objects.contentHighlight.cHighlight',
{
	extend			: 'Ext.grid.Panel',
	alias			: 'widget.ampleharvest-objects-contenthighlight-chighlight',
	title			: 'Highlight',
	requres			: [
	       			   		'AmpleHarvest.objects.contentHighlight.cToolbar'
	       			   ],
	columns			: [
	       			   		{
	       			   			header: 'Terms', 
	       			   			dataIndex : 'hl_target', 
	       			   			flex : 7
	       			   		},
	       			   		{
		       			   		header: 'Highlight', 
	       			   			dataIndex : 'hl_status', 
	       			   			flex : 3
	       			   		}
	       			   ],
//	store			: Ext.create('Ext.data.Store', {
//		fields 	: ['hl_target'],
//		storeID	: 'hl_store_id',
//		data 	: {},
//		     	       proxy: {
//		     	          type: 'memory',
//		     	          reader: {
//		     	              type: 'json',
//		     	              root: 'items'
//		     	          }
//		     	      }
//	}),
	bubbleEvents		:
	[
		'highlight'
	],
	hl_array		: [],
	listeners		:
	{
		'itemclick'	:
		{
			fn	: function(record, item, index, e)
			{
				var hl_id = item.get('hl_id');
				var hl_class = item.get('hl_class');
				var hl_attribute = 'entity_id';
				if(item.get('hl_status'))
				{
					this.hl_array[hl_id] = hl_class;
					hl_class = '';
					item.set('hl_status', false);
					
				} else {
					hl_class = this.hl_array[hl_id];
					item.set('hl_status', true);
				}
				this.fireEvent('highlight', hl_id, hl_class, hl_attribute);
			}
		}
	},
	
	hl_update		: function()
	{
		var store = this.getStore();
		store.each(function(record)
				{
					var hl_id = record.get('hl_id');
					var hl_class = '';
					var hl_attribute = 'entity_id';
					
					if(record.get('hl_status'))
					{	
						hl_class = record.get('hl_class');
					}
					this.fireEvent('highlight', hl_id, hl_class, hl_attribute);
				});
	},
	
	initComponent	: function()
	{
		var obj_This = this;
		
		this.store = Ext.create('Ext.data.Store', {
			fields 	: ['hl_target'],
			storeID	: 'hl_store_id',
			data 	: {},
			     	       proxy: {
			     	          type: 'memory',
			     	          reader: {
			     	              type: 'json',
			     	              root: 'items'
			     	          }
			     	      }
		});
		
		this.m_obj_Toolbar = Ext
		.create('AmpleHarvest.objects.contentHighlight.cToolbar');

		this.tbar = this.m_obj_Toolbar;
		
		this.callParent();
		
		
	},
	
	add_JsonObject	: function(JsonObject)
	{

		this.itemCount = 0;
		
		
		if(JsonObject)
		{
			for (val in JsonObject)
			{
				this.store.add(AmpleHarvest.DATA.Model.cMetadata.create(
						{
							hl_target	: JsonObject[val].hl_target,
							hl_class	: JsonObject[val].hl_class,
							hl_id		: JsonObject[val].hl_id,
							hl_status	: JsonObject[val].hl_status
						}));
				this.itemCount++;
			}
		}
	},
	
	hasData			: function()
	{
		if (!this.itemCount)
		{
			return false;
		}
		if (this.itemCount < 1)
		{
			return false;
		}
		this.setTitle(this.title + ' (' + this.itemCount + ')');
		return true;
	}
}
);
